package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.RedPacketRuleDao;
import com.sc.nft.entity.RedPacketRule;
import com.sc.nft.entity.dto.AdminRedPacketRuleUpdateByIdDTO;
import com.sc.nft.entity.dto.AdminRedPakcetRuleAddDTO;
import com.sc.nft.entity.vo.AdminRedPacketRuleFindPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.RedPacketRuleService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class RedPacketRuleServiceImpl implements RedPacketRuleService {

    private final RedPacketRuleDao redPacketRuleDao;


    /**
     * 根据藏品id和操作类型,获取对象数据集合
     *
     * @param collectionId
     * @param type
     */
    @Override
    public List<RedPacketRule> getByCollectionIdAndType(Long collectionId, Integer type) {
        return redPacketRuleDao.getByCollectionIdAndType(collectionId,type);
    }

    @Override
    public void add(AdminRedPakcetRuleAddDTO adminRedPakcetRuleAddDTO) {
        // 根据条件最大值,查询数据库最小值.查看是否有交叠
        Integer overlapping = redPacketRuleDao.overlapping(adminRedPakcetRuleAddDTO.getCollectionId(),adminRedPakcetRuleAddDTO.getType(),adminRedPakcetRuleAddDTO.getMin(),adminRedPakcetRuleAddDTO.getMax());
        if (overlapping>0) {
            throw new GlobalRunTimeException("参数有交叠,请检查");
        }
        if (adminRedPakcetRuleAddDTO.getMin()>adminRedPakcetRuleAddDTO.getMax()||adminRedPakcetRuleAddDTO.getMin().equals(adminRedPakcetRuleAddDTO.getMax())) {
            throw new GlobalRunTimeException("最大值必须大于最小值或者最小值不可以等于最大值");
        }
        RedPacketRule redPacketRule = new RedPacketRule();
        BeanUtil.copyProperties(adminRedPakcetRuleAddDTO,redPacketRule);
        redPacketRule.insert();
    }

    @Override
    public void updateById(AdminRedPacketRuleUpdateByIdDTO adminRedPakcetRuleAddDTO) {
        // 根据条件最大值,查询数据库最小值.查看是否有交叠
        Integer overlapping = redPacketRuleDao.overlappingUpdate(adminRedPakcetRuleAddDTO.getCollectionId(),adminRedPakcetRuleAddDTO.getType(),adminRedPakcetRuleAddDTO.getMin(),adminRedPakcetRuleAddDTO.getMax(),adminRedPakcetRuleAddDTO.getId());
        if (overlapping>0) {
            throw new GlobalRunTimeException("参数有交叠,请检查");
        }

        if (adminRedPakcetRuleAddDTO.getMin()>adminRedPakcetRuleAddDTO.getMax()||adminRedPakcetRuleAddDTO.getMin().equals(adminRedPakcetRuleAddDTO.getMax())) {
            throw new GlobalRunTimeException("最大值必须大于最小值或者最小值不可以等于最大值");
        }

        RedPacketRule redPacketRule = new RedPacketRule();
        BeanUtil.copyProperties(adminRedPakcetRuleAddDTO,redPacketRule);
        redPacketRule.updateById();
    }

    @Override
    public Page<AdminRedPacketRuleFindPageVO> findPage(PageRequest request) {
        return redPacketRuleDao.findPage(request);
    }

}
